﻿//----------------------------------------------------------------------------------------------------
// The Action Manager is responsible for handling actions
//----------------------------------------------------------------------------------------------------
package HG.Managers
{
	import HG.Debug;
	import HG.GameService;
	
	import HG.Managers.PlayerManager;
	import HG.Managers.InventoryManager;
	import HG.Managers.CameraManager;
	
	import HG.Actions.*;
	
	import HG.Visual.VisualNode;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	public class ActionManager extends IGameManager
	{
		public static const ACTION_HACK = 1;
		public static const ACTION_UNLOCK = 2;
		public static const ACTION_DOWNLOAD = 3;
		
		var m_turnManager:TurnManager;
		var m_cameraManager:CameraManager;
		
		var m_actionExecuteList:Array;
		
		var m_actionExecuteDelay:Timer;
		var m_actionSetupDelay:Timer;
		
		//----------------------------------------------------------------------------------------------------
		public function ActionManager():void
		{
			m_actionExecuteList = new Array();
			
			m_actionExecuteDelay = new Timer(2000, 1);
			m_actionExecuteDelay.addEventListener(TimerEvent.TIMER, ExecuteNextAction);
			
			m_actionSetupDelay = new Timer(1000, 1);
			m_actionSetupDelay.addEventListener(TimerEvent.TIMER, SetUpNextAction);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Initialize()
		{
			m_turnManager = GameService.GetInstance().GetTurnManager();
			m_cameraManager = GameService.GetInstance().GetCameraManager();
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ExecuteActions(actions:Array)
		{
			Debug.ASSERT(m_actionExecuteList.length <= 0, "Attempt to execute actions before previous set complete!");
			
			m_actionExecuteList = actions;
			
			SetUpNextAction(null);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function SetUpNextAction(evt:TimerEvent)
		{
			if (m_actionExecuteList.length > 0)
			{
				var action:IAction = m_actionExecuteList[0];
				
				m_cameraManager.LookAtNode(action.GetTargetNodeID());
				
				m_actionExecuteDelay.start();
			}
			else
			{
				m_turnManager.EndTurn();
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function ExecuteNextAction(evt:TimerEvent)
		{
			var action:IAction = m_actionExecuteList[0];
			m_actionExecuteList.splice(0, 1);
			
			// Execute the action and ensure it is successfull.
			var success = action.Execute();
			Debug.ASSERT(success, "Action failed to Execute: " + action.GetLastError());
			
			if (!action.GetIsComplete())
			{
				m_turnManager.AddPendingRepeatedAction(action);
			}
			
			m_actionSetupDelay.start();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetActionName(type:Number):String
		{
			switch(type)
			{
				case ACTION_HACK:
				{
					return "COMPROMISE";
				}break;
				
				case ACTION_UNLOCK:
				{
					// TODO
					return "DECRYPT"
				}break;
				
				case ACTION_DOWNLOAD:
				{
					// TODO
					return "DOWNLOAD"
				}break;
			}
			
			return "";
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetActionDesc(type:Number):String
		{
			switch(type)
			{
				case ACTION_HACK:
				{
					return "Allow access to this nodes resources.";
				}break;
				
				case ACTION_UNLOCK:
				{
					// TODO
					return "Remove encryption from this node."
				}break;
				
				case ACTION_DOWNLOAD:
				{
					// TODO
					return "Search for any useful information."
				}break;
			}
			
			return "";
		}
		
		//----------------------------------------------------------------------------------------------------
		public function CreateAction(type:Number, playerID, nodeID:Number):IAction
		{
			switch(type)
			{
				case ACTION_HACK:
				{
					return new ActionHack(playerID, nodeID, ACTION_HACK);
				}break;
				
				case ACTION_UNLOCK:
				{
					// TODO
					return new ActionUnlock(playerID, nodeID, ACTION_UNLOCK);
				}break;
				
				case ACTION_DOWNLOAD:
				{
					// TODO
					return new ActionDownload(playerID, nodeID, ACTION_DOWNLOAD);
				}break;
			}
			
			Debug.ASSERT(false, "Attempt to create bad action");
			return null;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetActionCost(type:Number):Number
		{
			return 1;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function CheckActionRequirements(type:Number, visNode:VisualNode):Boolean
		{
			var inventoryManager:InventoryManager = GameService.GetInstance().GetInventoryManager();
			var playerManager:PlayerManager = GameService.GetInstance().GetPlayerManager();
						
			switch(type)
			{
				case ACTION_HACK:
				{
					return true;
				}break;
				
				case ACTION_UNLOCK:
				{
					var encryptionKey = visNode.GetEncryptionKey();
					
					if (encryptionKey != 0)
					{
						var playerID:Number = playerManager.GetCurrentPlayerID();
						if (inventoryManager.GetPlayerHasEncryptionKey(playerID, encryptionKey))
						{
							return true;
						}
					}
					return false;
					
				}break;
				
				case ACTION_DOWNLOAD:
				{
					return true;
				}break;
			}
			
			return true;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}





